/*
 * ssa.h
 *
 *  Created on: Jul 21, 2010
 *      Author: vincent
 */

#ifndef SSA_H
#define SSA_H

#include "flow.h"
#include <vector>


/// dynamic bitset in STL
typedef std::vector<bool>   dyn_bitset;

/// set1 |= set2
dyn_bitset& bitsetOr(dyn_bitset& set1 , dyn_bitset& set2);

/// set1 &= ~set2
dyn_bitset& bitsetAndNot(dyn_bitset& set1 , dyn_bitset& set2);

class LiveNode : public AnalysisNode{
public:
    LiveNode(BasicBlock* bb,int size);

    dyn_bitset  *liveIn;
    //dyn_bitset  *liveOut;

    /// the set of variables used in s before any assignment
    dyn_bitset  *gen;

    /// the set of variables assigned a value in s before any use
    dyn_bitset  *killed;

    /// variable in varIndex is defined
    void    define(int varIndex);

    /// variable is varIndex is used
    void    use(int varIndex);
};

class LiveAnalysis : public Analysis<LiveNode>{
private:
    int localVarNum;

    /// calculate the gen and killed set of a basicblock
    void    scan(BasicBlock *bb);

    void    scanBlocks();
public:
    LiveAnalysis(CFG *cfg);

    virtual LiveNode*   createNode(BasicBlock *bb);
    /// calculate the live-in info for each bb
    void buildLiveness();
    void printBlocks();
};

#endif // SSA_H
